using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using TVDesigner.GUI.Elements;

namespace TVDesigner.GUI.Controls
{
    public partial class DesignPanel : UserControl
    {
        #region Atributos
        private Element _FocusElement;
        private DiagramPanel _Diagram;
        private FMain _MainForm;
        #endregion

        #region Propriedades

        public Elements.Element FocusElement
        {
            get { return _FocusElement; }
            set
            {
                _FocusElement = value;
                if (_FocusElement != null)
                {
                    _FocusElement.BringToFront();
                }
                _Panel.Invalidate();
            }
        }

        public List<Element> Elements
        {
            get
            {
                List<Element> elements = new List<Element>();
                foreach (Control control in _Panel.Controls)
                {
                    if (control is Element)
                    {
                        elements.Add(control as Element);
                    }
                }
                return elements;
            }
        }

        public DiagramPanel Diagram
        {
            get { return _Diagram; }
            set { _Diagram = value; }
        }

        public FMain MainForm
        {
            get { return _MainForm; }
            set { _MainForm = value; }
        }

        #endregion


        public DesignPanel()
        {
            InitializeComponent();

            _ToolBox.Items.AddRange(
                new System.Windows.Forms.ToolStripItem[] {
                    new TVDesigner.GUI.Controls.Toolbox.ToolboxButton(this, TVDesigner.GUI.Controls.Toolbox.EToolboxType.Text),
                    new TVDesigner.GUI.Controls.Toolbox.ToolboxButton(this, TVDesigner.GUI.Controls.Toolbox.EToolboxType.Image),
                    new TVDesigner.GUI.Controls.Toolbox.ToolboxButton(this, TVDesigner.GUI.Controls.Toolbox.EToolboxType.Button),
                    new TVDesigner.GUI.Controls.Toolbox.ToolboxButton(this, TVDesigner.GUI.Controls.Toolbox.EToolboxType.Table),
                    new System.Windows.Forms.ToolStripSeparator(),
                    new TVDesigner.GUI.Controls.Toolbox.ToolboxButton(this, TVDesigner.GUI.Controls.Toolbox.EToolboxType.ScreenResize),
                    new System.Windows.Forms.ToolStripSeparator(),
                    new TVDesigner.GUI.Controls.Toolbox.ToolboxButton(this, TVDesigner.GUI.Controls.Toolbox.EToolboxType.Remove)
                }
            );
        }
        public void AddNewElement(Element element, bool hasFocus)
        {
            if (_Diagram.Screen.AddGenericComponent(element.LogicElement))
            {
                this.AddElement(element, hasFocus);
            }
        }

        public void AddElement(Element element, bool hasFocus)
        {
            element.AddClickEvent(new MouseEventHandler(_Element_MouseDown));            
            _Panel.Controls.Add(element);
            
            _MainForm.FocusElement = element;

            if (hasFocus)
            {
                _MainForm.ListPropertyPanel.ReloadCombo();
                FocusElement = element;
            }
        }

        public void RemoveElement(TVDesigner.GUI.Elements.Element element)
        {
            if (element != null)
            {
                _Diagram.Screen.RemoveGenericComponent(element.LogicElement);
                _Panel.Controls.Remove(element);
                _MainForm.FocusElement = null;
                FocusElement = null;
            }
        }

        public void RemoveSelectedElement()
        {
            this.RemoveElement(_FocusElement);
        }

        protected void _Element_MouseDown(object sender, MouseEventArgs e)
        {
            Control c = sender as Control;
            FocusElement = c.Parent as Element;
            _MainForm.FocusElement = _FocusElement;
        }

        private void _Panel_Click(object sender, EventArgs e)
        {
            FocusElement = null;
            _MainForm.FocusElement = null;
        }

        private void _Panel_Paint(object sender, PaintEventArgs e)
        {
            if (_FocusElement != null && !_FocusElement.IsDragging)
            {
                Rectangle rect = new Rectangle(
                    _FocusElement.Location.X - 2,
                    _FocusElement.Location.Y - 2,
                    _FocusElement.Width + 2,
                    _FocusElement.Height + 2);

                e.Graphics.DrawRectangle(new Pen(Brushes.Black, 3), rect);
            }
        }
    }
}
